﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using EasyComm.Hosting.Extension;

namespace EasyComm.Hosting.Behavior
{
    public class SLBDiscoveryBehavior : BehaviorExtensionElement, IServiceBehavior
    {
        private Uri _discoveryProxyAddress;
        private Type _workerAdminContract;

        public SLBDiscoveryBehavior(Type workerAdminContract, Uri discoveryProxyAddress)
        {
            _discoveryProxyAddress = discoveryProxyAddress;
            _workerAdminContract = workerAdminContract;
        }

        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {

        }

        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            SLBDiscoveryExtension discoveryExtension = new SLBDiscoveryExtension(_workerAdminContract, _discoveryProxyAddress);
            serviceHostBase.Extensions.Add(discoveryExtension);
        }

        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {

        }

        public override Type BehaviorType
        {
            get { return typeof(SLBDiscoveryBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new SLBDiscoveryBehavior(_workerAdminContract, _discoveryProxyAddress);
        }
    }
}